package com.mmall.concurrence.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class CyclicBarrierExample1 {


    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(5);
    public static void main(String[] args) throws Exception {

        ExecutorService exe = Executors.newCachedThreadPool();

        for (int i=0;i<10;i++){
            final int threadNum = i;
            Thread.sleep(1000);
            exe.execute(()->{
                exe.execute(()->{
                    try {
                        race(threadNum);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
            });
        }
        exe.shutdown();
    }

    private static void race(int threadNum) throws Exception{
        Thread.sleep(1);
        log.info("{} is ready",threadNum);
        cyclicBarrier.await();
        log.info("{} is continue",threadNum);
    }


}
